var Subscribe = function () {

    this.$jiraSubcribeStatusSelect = $("#jira_subcribe_status_select");
    this.$jiraSubcribeOpenCheckbox = $("#jira_subcribe_open_checkbox");
    this.$jiraSubcribeSubmit = $('#jira_subcribe_submit');

    // 请求url
    this.URL = {
        MODIFY: '/jira/subscribe/modify',
        SUBSCRIBE_GET_ALL: '/jira/issue/status',
        SUBSCRIBE_GET_MINE: '/jira/subscribe/get/mine',
        SUBSCRIBE_CHANGE_ADD: '/jira/subscribe/add',
        SUBSCRIBE_CHANGE_DEL: '/jira/subscribe/del'
    };

};

Subscribe.prototype = {
    // 初始化表格
    initGird: function () {

        var self = this;

        $.xumf.ajaxAndLoading({
            type: 'get',
            url: self.URL.SUBSCRIBE_GET_MINE
        }).done(function (res) {
            if (res.success) {

                var data = res.data;
                self.$jiraSubcribeStatusSelect.xumf_select({
                    url: self.URL.SUBSCRIBE_GET_ALL
                }).on('xumf.select.success', function () {
                    $(this).multiselect({
                        includeSelectAllOption: true,
                        selectAllText: '全选',
                        allSelectedText: '全选',
                        nonSelectedText: '未选',
                        nSelectedText: '项 已选择 '
                    });
                    if (undefined === data) {
                        self.$jiraSubcribeOpenCheckbox.prop('checked', false);
                    } else {
                        self.$jiraSubcribeOpenCheckbox.prop('checked', true);
                        $(this).multiselect('select', data.status.split(','));
                    }
                });
            } else {
                $.xumf.gritter(res);
            }
        });

        this.$jiraSubcribeSubmit.on('click', function (event) {
            event.preventDefault();
            var checked = self.$jiraSubcribeOpenCheckbox.prop('checked');
            var statusObj = self.$jiraSubcribeStatusSelect.val();
            var status;
            if (null === statusObj) {
                status = '';
            } else {
                status = statusObj.join(',');
            }
            if (checked) {
                $.xumf.ajaxAndLoading({
                    url: self.URL.SUBSCRIBE_CHANGE_ADD,
                    data: {status: status}
                }).done(function (res) {
                    $.xumf.gritter(res);
                });
            } else {
                $.xumf.ajaxAndLoading({
                    url: self.URL.SUBSCRIBE_CHANGE_DEL
                }).done(function (res) {
                    $.xumf.gritter(res);
                });
            }
        });


    }
};

